import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'autoMonitorM',

  state: {
    poolNameData: {},
    selectData: {},
    currentUser: {},
    applyData: {},
    detailData: {},
    tagData: {},
    poolName: [],
    tableData: { columns: [], dataSource: [], pageInfo: { currentPage: 0, numInPage: 20, total: 1 } },
  },

  effects: {
    //获取回填数据
    *getDetails({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getDetails, payload);

      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            detailData: res.data.dataSource,
          },
        });
        callback(res.data);
      } else {
        message.error('查询列表失败');
      }
    },

    // 获取自动入池表格数据
    *getTableData({ payload, callback }, { call, put }) {
      const res = yield call(api.autoMonitor.getTableData, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: res.data,
          },
        });
      } else {
        message.error('请求失败');
      }
    },

    // 获取查看页面和重新发起券池名称
    *getPoolName({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getPoolName, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            poolNameData: res,
          },
        });
      } else {
        message.error('获取券池名称失败');
      }
    },

    // 获取主页面券池名称
    *searchAll({ payload, callback }, { call, put }) {
      const res = yield call(api.definition.saerchAll, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            poolName: res.data.dataSource,
          },
        });
        //   callback(res.data)
      } else {
        message.error('获取券池名称失败');
      }
    },

    // 自动新增and修改
    *autoAdd({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.autoAdd, payload);
      if (res && res.code == 0) {
        callback(res);
        message.success('保存成功');
      } else {
        message.error(res.msg);
      }
    },

    // 获取标签
    *findTag({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findTag, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            tagData: res,
          },
        });
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
